package com.ochnas.volunteersignup.ui;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.ochnas.volunteersignup.domain.User;
import com.ochnas.volunteersignup.service.UserService;

public class EmailActivationService extends HttpServlet {
	
	WebApplicationContext spring;
    private static final long serialVersionUID = -2560989072138239258L;

	public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
        spring = WebApplicationContextUtils.getWebApplicationContext(servletConfig.getServletContext());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       UserService userService = (UserService) spring.getBean("userService");
    	String emailAddress = request.getParameter("userEmail");
    	String activationKey = request.getParameter("activationKey");
    	userService.activateUserAccount(emailAddress, activationKey);
    	response.setContentType("text/xml");
    	User activeUser = userService.findUserAccountByEmailAddress(emailAddress);
    	response.getOutputStream().println(activeUser.toXml()); 
    	
        }
}



